#include "dtwentyitem.h"


#include <QMetaProperty>


DTwentyItem::DTwentyItem(const QString &name, QObject* parent) :
    AbstractModelItem(parent),
    d(new Data())
{
    d->name = name;
}

DTwentyItem::DTwentyItem(const DTwentyItem &other) :
    AbstractModelItem(other),
    d(other.d)
{
}


QVariant DTwentyItem::value(int field, int role) const
{
    if (field < 0 || field >= fieldCount())
    {
        return QVariant();
    }

    switch (role)
    {
    case Qt::DisplayRole:
    case Qt::EditRole:
    {
        const QMetaObject* mo = metaObject();
        QMetaProperty mp = mo->property(field+1);
        return mp.read(this);
    }


    default:
        break;
    }

    return QVariant();

}

bool DTwentyItem::setValue(int field, const QVariant &value, int role)
{
    if (field < 0 || field >= fieldCount())
    {
        return false;
    }

    switch (role)
    {
    case Qt::EditRole:
    {
        const QMetaObject* mo = metaObject();
        QMetaProperty mp = mo->property(field+1);
        return mp.write(this, value);
    }

    default:
        break;
    }

    return false;
}

int DTwentyItem::fieldCount() const
{
    return metaObject()->propertyCount() - 1;
}

QVariant DTwentyItem::fieldName(int field) const
{
    if (field < 0 || field >= fieldCount())
    {
        return QVariant();
    }

    const QMetaObject* mo = metaObject();
    QMetaProperty mp = mo->property(field+1);

    return mp.name();
}


bool DTwentyItem::operator ==(const QString &name)
{
    return (d->name == name);
}

bool DTwentyItem::operator ==(const DTwentyItem &other)
{
    return (d->name == other.name());
}

bool DTwentyItem::operator <(const DTwentyItem &other)
{
    return (d->name < other.name());
}

bool DTwentyItem::operator <(const QString &name)
{
    return (d->name < name);
}

QString DTwentyItem::name() const
{
    return d->name;
}

void DTwentyItem::setName(const QString &name)
{
    if (name == d->name)
    {
        return;
    }

    d->name = name;
    emit sigChanged();
}


int DTwentyItem::childType() const
{
    return -1;
}


